<?php
class Pageley_Util_ArrayMapTests extends Pageley_Test_Case {
	
    public function testConstruct() {
        $map = new Pageley_Util_ArrayMap(1, 2, 3);
        $this->assertEquals(3, $map->size());
        $this->assertEquals(1, $map->get(0));
        $this->assertEquals(2, $map->get(1));
        $this->assertEquals(3, $map->get(2));
    }
    
    public function testAddAndGet() {
		$map = new Pageley_Util_ArrayMap();
		$map->add("foo");
		$map->add("bar");
		$this->assertEquals("foo", $map->get(0));
		$this->assertEquals("bar", $map->get(1));
		$this->assertNull($map->get(2));
	}

	public function testIteration() {
		$map = new Pageley_Util_ArrayMap();
		$map->add("foo");
		$map->add("bar");
		$i = 0;
		foreach($map as $item) {
			if($i === 0) {
				$this->assertEquals("foo", $item);
			} else {
				$this->assertEquals("bar", $item);
			}
			$i++;
		}
	}

	public function testPut() {
		$map = new Pageley_Util_ArrayMap();
		$map->put("foo", "bar");
		$this->assertEquals("bar", $map->get("foo"));
		$this->assertNull($map->get("foobar"));
	}

	public function testSize() {
		$map = new Pageley_Util_ArrayMap();
		$this->assertEquals(0, $map->size());
		$map->add("foo");
		$this->assertEquals(1, $map->size());
		$map->add("bar");
		$this->assertEquals(2, $map->size());
	}

	public function testContainsKey() {
		$map = new Pageley_Util_ArrayMap();
		$map->put("foo", "bar");
		$this->assertTrue($map->containsKey("foo"));
		$this->assertFalse($map->containsKey("foobar"));
	}

	public function testContainsValue() {
		$map = new Pageley_Util_ArrayMap();
		$map->put("foo", "bar");
		$this->assertTrue($map->containsValue("bar"));
		$this->assertFalse($map->containsValue("foobar"));
	}

	public function testRemove() {
		$map = new Pageley_Util_ArrayMap();
		$map->put("foo", "bar");
		$map->remove("foo");
		$this->assertTrue($map->isEmpty());
	}

	public function testRemoveValue() {
		$map = new Pageley_Util_ArrayMap();
		$map->put("foo", "bar");
		$map->removeValue("bar");
		$this->assertTrue($map->isEmpty());
	}
	
	public function testSort() {
		$entry1 = new Pageley_Util_Entry(1, "Bob");
		$entry2 = new Pageley_Util_Entry(2, "William");
		$entry3 = new Pageley_Util_Entry(3, "Bill");
		$map = new Pageley_Util_ArrayMap();
		$map->add($entry1);
        $map->add($entry2);
		$map->add($entry3);
		$map->sort();
		$this->assertEquals("Bob", $map->get(0)->getValue());
		$this->assertEquals("William", $map->get(1)->getValue());
		$this->assertEquals("Bill", $map->get(2)->getValue());
	}
}